package com.demo.core.dao.impl;

import java.util.List;
import java.util.Map;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.FormDao;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.Form;
import com.demo.core.model.support.GeneralExecuteModel;

/**
 * 自定义表单数据访问层实现类
 * 
 * @author ryuu.kk
 */
public class FormDaoImpl extends PageCoreDaoSupport<Form, Long> implements FormDao {
	
	/**
	 * 自定义表单空间 
	 */
	protected final static String SQLMAP_NAME_SPACE = "formSpace.";
	
	/**
	 * 数据库的表名
	 */
	protected final static String TABLE_NAME = "T_CORE_FORM";
	
	/**
	 * 数据库表的主键名称
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	public int updateStatus(Form form, Map<String, Object> params) {
		cacheClear();
		params.put("form", form);
		return getSqlMapClientTemplate().update(SQLMAP_NAME_SPACE + "changeEnabled", params);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Form> tree(Map<String, Object> params) {
		List<Form> result = null;
		CacheKey key = null;
		int hashCode = params.hashCode();
		CacheWrapper<CacheKey, Object> cache = null;
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			//key.update("search");
			key.update(SQLMAP_NAME_SPACE);
			key.update("tree");
			key.update(hashCode);
			result = (List<Form>) cache.get(key);
		}
		if (result != null) {
			return result;
		}
		//params.put("form", form);
		List<Form> formList = getSqlMapClientTemplate().queryForList(SQLMAP_NAME_SPACE + "tree", params);
		if (isUseCache() && (cache = getCache()) != null) {
			cache.put(key, result);
		}
		return formList;
	}

	@Override
	protected String nameSpace() {
		return model.getNameSpace();
	}

	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
		model.setNameSpace(SQLMAP_NAME_SPACE);
	}
}